<!-- 网络类型列表显示 -->
<template>
    <div>
        <div class="common-layout">
            <el-container>
                <el-aside width="200px" style="padding-top: 3px;max-height: 100%">
                    <div style="border: 1px solid cadetblue;height: 100%;">
                        <!-- 查询 -->
                        <el-input v-model="filterText" style="width: 100%;padding-bottom: 12px;" placeholder="请输入" />
                        <!-- 树形列表 -->
                        <el-tree ref="treeRef" style="max-width: 600px" class="filter-tree" :data="data"
                            default-expand-all node-key="value" highlight-current :filter-node-method="filterNodes" />
                    </div>
                </el-aside>
                <el-container>
                    <el-header>
                        <!-- 查询条件 -->
                        <div style="width: 100%;padding-top: 20px;border: 1px solid cadetblue">
                            <el-form :inline="true" :model="ShowData" class="demo-form-inline"
                                style="text-align: center;">
                                <el-form-item label="网格名称">
                                    <el-input v-model="ShowData.Grid_name" placeholder="请输入网格名称" clearable />
                                </el-form-item>
                                <el-form-item>
                                    <el-button type="primary" @click="fetchData">搜索</el-button>
                                    <el-button type="" @click="chengzho">重置</el-button>

                                </el-form-item>
                            </el-form>
                        </div>

                    </el-header>
                    <el-main>
                        <el-button type="primary" @click="dialogVisible = true, resetForm(ruleFormRef)">新增</el-button>
                        <!--列表 -->

                        <el-table :data="tableData" style="width: 100%; border: 1px solid cadetblue"
                            :row-class-name="tableData">
                            <el-table-column type="index" label="序号" width="100px" align="center" />
                            <el-table-column prop="grid_number" label="网格编号" />
                            <el-table-column prop="grid_name" label="网络名称" />
                            <el-table-column prop="subordinate" label="所属区域">
                                <template v-slot="scope">
                                    <span v-if="scope.row.owning_region == 1">呼兰街道</span>
                                    <span v-if="scope.row.owning_region == 2">兰河街道</span>
                                    <span v-if="scope.row.owning_region == 3">利民街道</span>
                                </template>
                            </el-table-column>
                            <el-table-column prop="subordinate" label="所属层级" />
                            <el-table-column label="操作">
                                <template v-slot="scope">
                                    <el-button type="primary" @click="handleEdit(scope.row.id)">编辑</el-button>
                                    <el-button type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-main>
                </el-container>
            </el-container>
        </div>
        <!-- 分页 -->
        <table style="margin: 5px auto;">
            <tbody>
                <tr>
                    <td>
                        <el-pagination v-model:current-page="page.page" v-model:page-size="page.size"
                            :page-sizes="[15, 20, 25, 30]" background layout="total,slot" :total="page.tatalCount"
                            @size-change="handleSizeChange" @current-change="handleCurrentChange" prev-text="上一页"
                            next-text="下一页">
                            <span>
                                &nbsp;可以显示{{ page.size }}条，一共{{ page.pageCount }}页，当前是第{{ page.page }}页
                            </span>
                        </el-pagination>
                    </td>
                    <td>
                        <el-pagination v-model:current-page="page.page" v-model:page-size="page.size"
                            :page-sizes="[15, 20, 25, 30]" background layout="sizes, slot,prev, pager, next"
                            :total="page.tatalCount" @size-change="handleSizeChange"
                            @current-change="handleCurrentChange" prev-text="上一页" next-text="下一页">
                        </el-pagination>
                    </td>
                    <td>
                        <el-pagination v-model:current-page="page.page" v-model:page-size="page.size"
                            :page-sizes="[15, 20, 25, 30]" background layout="slot,jumper" :total="page.tatalCount"
                            @size-change="handleSizeChange" @current-change="handleCurrentChange" prev-text="上一页"
                            next-text="下一页">
                        </el-pagination>
                    </td>
                </tr>
            </tbody>
        </table>
        <!-- 弹出层 -->
        <el-dialog v-model="dialogVisible" title="新增单元网格" width="500">
            <span>
                <!-- 表单 -->
                <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto"
                    class="demo-ruleForm" status-icon>
                    <el-form-item label="网格类别" prop="grid_class">
                        <el-input v-model="ruleForm.grid_class" disabled />
                    </el-form-item>
                    <el-form-item label="所属街道" prop="owning_region">
                        <el-select v-model="ruleForm.owning_region" placeholder="请输入所属街道">
                            <el-option v-for="item in AreaList" :key="item.value" :label="item.text"
                                :value="item.value" />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="网络编码" prop="grid_number">
                        <el-input v-model="ruleForm.grid_number" />
                    </el-form-item>
                    <el-form-item label="网格名称" prop="grid_name">
                        <el-input v-model="ruleForm.grid_name" />
                    </el-form-item>
                    <el-form-item label="所属层级" prop="subordinate">
                        <el-input v-model="ruleForm.subordinate" disabled />
                    </el-form-item>
                    <el-form-item label="网格面积" prop="grid">
                        <el-input v-model="ruleForm.grid" />
                    </el-form-item>
                </el-form>
            </span>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm(ruleFormRef)">
                        保存
                    </el-button>
                    <el-button @click="resetForm(ruleFormRef)">重置</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup lang="ts">
//引入axios
import axios from 'axios';
//引入组件
import { ref, reactive, onMounted, watch } from "vue";

import type { FormInstance, FormRules } from 'element-plus'

import { ElTree } from 'element-plus'

import { ElMessage, ElMessageBox } from 'element-plus'

const treeRef = ref<InstanceType<typeof ElTree>>()

const filterNodes = (value: string, ShuTrrList: Tree) => {
    if (!value) return true
    return ShuTrrList.label.includes(value)
}
const dialogVisible = ref(false)

const filterText = ref('')
const ruleFormRef = ref<FormInstance>()

watch(filterText, (val) => {
    treeRef.value!.filter(val)
})


//表单
const ruleForm = reactive({
    id: 0,
    "grid_class": "物理网格",
    "owning_region": "",
    "grid_number": "",
    "grid_name": "",
    "subordinate": "三级网格",
    "grid": "",
    "boundary": "",//绘制面积
})


//编辑
const handleEdit = (id: number) => {
    dialogVisible.value = true
    axios.get("http://10.31.60.18:8000/GriddingGetRout/api/Cellgrid/FanCity?Id=" + id).then(res => {
        ruleForm.id = res.data.id;
        ruleForm.grid_class = res.data.grid_class;
        ruleForm.owning_region = res.data.owning_region;
        ruleForm.grid_number = res.data.grid_number;
        ruleForm.grid_name = res.data.grid_name;
        ruleForm.subordinate = res.data.subordinate;
        ruleForm.grid = res.data.grid;
        ruleForm.boundary = res.data.boundary;
    })
}

//删除
const handleDelete = (id: number) => {
    ElMessageBox.confirm(
        '确定要删除吗？',
        'Warning',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            axios.delete("http://47.95.178.186:8000/GriddingSetsRout/api/Cellgrid/GriddingDel?Id=" + id).then(res => {
                if (res.data > 0) {
                    ElMessage.success("删除成功!");
                    fetchData();
                    return;

                } if (res.data > 0) {
                    ElMessage.error("删除失败！");
                    fetchData();
                    return;
                }
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: 'Delete canceled',
            })
        })
}


//验证
const rules = reactive<FormRules>({
    grid_class: [
        { required: true, message: '网格类别不能为空！', trigger: 'blur' },
        { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
    ],
    owning_region: [
        {
            required: true,
            message: '所属街道不能为空！',
            trigger: 'change',
        },
    ],
    grid_number: [
        { required: true, message: '网络编码不能为空！', trigger: 'blur' },
        { pattern: /^[A-Za-z0-9]{4,40}$/, message: '生产许可证只能为数字,字母组合', trigger: 'blur' },
    ],
    grid_name: [
        { required: true, message: '网格名称不能为空！', trigger: 'blur' },
        { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
    ],
    subordinate: [
        { required: true, message: '所属层级不能为空！', trigger: 'blur' },
        { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
    ],
    grid: [
        { required: true, message: '网格面积不能为空！', trigger: 'blur' },
        { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
    ],
})

//保存按钮
const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            if (ruleForm.id == 0) {
                axios.post("http://47.95.178.186:8000/GriddingSetsRout/api/Cellgrid/CellgridCreate", ruleForm).then(res => {
                    if (res.data > 0) {
                        ElMessage.success("新增成功！");
                        dialogVisible.value = false;
                        fetchData();
                        return;
                    } else {
                        ElMessage.error("新增失败！");
                        return;
                    }
                })
            } else {
                axios.put("http://47.95.178.186:8000/GriddingSetsRout/api/Cellgrid/GriddingUpd", ruleForm).then(res => {
                    if (res.data > 0) {
                        ElMessage.success("修改成功！");
                        dialogVisible.value = false;
                        fetchData();
                        return;
                    } else {
                        ElMessage.error("修改失败！");
                        return;
                    }
                })
            }
        } else {
            console.log('error submit!', fields)
        }
    })
}

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}

interface Tree {
    [key: string]: any
}


//重置按钮
const chengzho = () => {
    ShowData.Grid_name = "";
    ShowData.Owning_region = "";
    fetchData();
}

//分页方法
const handleSizeChange = (val: number) => {
    page.size = val;
    fetchData();
}
const handleCurrentChange = (val: number) => {
    page.page = val;
    fetchData();
}
const tableData = ref([])

const page = reactive({
    page: 1,
    size: 15,
    tatalCount: 0,
    pageCount: 0,
})

//所属区域
const AreaList = ref<{ value: number; text: string }[]>([]);

const Area = () => {
    axios.get("http://47.95.178.186:8000/GriddingGetRout/Cellgrid/AreaType").then(res => {
        AreaList.value = res.data;
    })
}


const data: Tree[] = [
    {
        label: '呼兰区',
        children: [
            {
                label: '呼兰街道',
                children: [
                    {
                        label: '东风社区',
                    },
                    {
                        label: '文化社区',
                    },
                    {
                        label: '新民社区',
                    },
                ]
            },
            {
                label: '利民街道',

            },
            {
                label: '兰河街道',

            },
        ],
    },
]



//条件查询的表单
const ShowData = reactive({
    Grid_name: "",  //查询条件网络类型名称
    Owning_region: "",
});

//异步显示列表
const fetchData = async () => {
    try {
        //response
        const res = await axios.get('http://47.95.178.186:8000/GriddingGetRout/api/Cellgrid/PageShowCellgrid', {
            params: {
                PageIndex: page.page,
                PageSize: page.size,
                Grid_name: ShowData.Grid_name,
                Owning_region: ShowData.Owning_region || 0,
            }
        })

        tableData.value = res.data.data
        page.tatalCount = res.data.totalCount
        page.pageCount = res.data.pageCount
    } catch (error) {
        console.error(error)
    }
}
//首次加载显示列表信息
onMounted(() => {
    fetchData();
    Area();
})

</script>

<style scoped>
.container {
    /* 设置div居中对齐 */
    width: 100%;
    display: flex;
    justify-content: center;
}

.table-container {
    /* 设置table居中对齐 */
    display: flex;
    justify-content: center;
}
</style>